package com.surelution.projects.crm
import org.codehaus.groovy.grails.commons.ConfigurationHolder

class StationPictureController {
	def pictureService
	
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [stationPictureInstanceList: StationPicture.list(params), stationPictureInstanceTotal: StationPicture.count()]
    }

    def create = {
        def stationPictureInstance = new StationPicture()
        stationPictureInstance.properties = params
        return [stationPictureInstance: stationPictureInstance]
    }

    def save = {
        def stationPictureInstance = new StationPicture(params)
        if (stationPictureInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), stationPictureInstance.id])}"
            redirect(action: "show", id: stationPictureInstance.id)
        }
        else {
            render(view: "create", model: [stationPictureInstance: stationPictureInstance])
        }
    }

    def show = {
        def stationPictureInstance = StationPicture.get(params.id)
        if (!stationPictureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), params.id])}"
            redirect(action: "list")
        }
        else {
            [stationPictureInstance: stationPictureInstance]
        }
    }

    def edit = {
        def stationPictureInstance = StationPicture.get(params.id)
        if (!stationPictureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [stationPictureInstance: stationPictureInstance]
        }
    }

    def update = {
        def stationPictureInstance = StationPicture.get(params.id)
        if (stationPictureInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (stationPictureInstance.version > version) {
                    
                    stationPictureInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'stationPicture.label', default: 'StationPicture')] as Object[], "Another user has updated this StationPicture while you were editing")
                    render(view: "edit", model: [stationPictureInstance: stationPictureInstance])
                    return
                }
            }
            stationPictureInstance.properties = params
			stationPictureInstance.picture.title=params.title
			stationPictureInstance.picture.description=params.description
			
            if (!stationPictureInstance.hasErrors() && stationPictureInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), stationPictureInstance.id])}"
                redirect(action: "show", id: stationPictureInstance.id)
            }
            else {
                render(view: "edit", model: [stationPictureInstance: stationPictureInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def stationPictureInstance = StationPicture.get(params.id)
		def sId = stationPictureInstance.station.id
		def sName = stationPictureInstance.station.name
		println OwnStation.findByName(sName)
        if (stationPictureInstance) {
            try {
                stationPictureInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), params.id])}"
				if(OwnStation.findByName(sName)){
					redirect(action: "show",controller:"ownStation",id:sId)
				}
				if (CompetitorStation.findByName(sName)){
					redirect(action: "show",controller:"competitorStation",id:sId)
				}
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), params.id])}"
				if(OwnStation.get(sId)){
					redirect(action: "show",controller:"ownStation",id:sId)
				}
				if (CompetitorStation.get(sId)){
					redirect(action: "show",controller:"competitorStation",id:sId)
				}
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'stationPicture.label', default: 'StationPicture'), params.id])}"
            redirect(action: "list")
        }
    }

	def name = {
        def stationPicture = StationPicture.get(params.id)
		def picture = stationPicture.picture
		def filePath = pictureService.loadPicture(picture)
		
		FileInputStream fos = new FileInputStream(filePath)
		response.contentType = "image/${picture.type}"
		response.outputStream << fos
		response.outputStream.flush()
		fos.close()
		return
	}
	
}
